package com.itheima.consumer.controller;

import com.itheima.consumer.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 服务的调用方
 */

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    
    @GetMapping("/goods3/{id}")
    public Goods findGoodsById3(@PathVariable("id") int id) {
        System.out.println("findGoodsById..." + id);
        
        //远程调用Goods服务中的findOne接口,方法1,通过
        String uri = "http://localhost:8000/goods/findOne/1";
        Goods goods = restTemplate.getForObject(uri, Goods.class);
        return goods;
    }
    
    @GetMapping("/goods2/{id}")
    public Goods findGoodsById2(@PathVariable("id") int id) {
        System.out.println("findGoodsById..." + id);
        
        //
        List<ServiceInstance> list = discoveryClient.getInstances("eureka-provider");
        if (list == null || list.isEmpty()) {
            return null;
        }
        ServiceInstance instance = list.get(0);
        String host = instance.getHost();
        System.out.println(host);
        int port = instance.getPort();
        System.out.println(port);
        //远程调用Goods服务中的findOne接口,方法1,通过
        String uri = "http://" + host + ":" + port + "/goods/findOne/1";
        Goods goods = restTemplate.getForObject(uri, Goods.class);
        return goods;
    }
    
    @Autowired
    private DiscoveryClient discoveryClient;
    
    @GetMapping("/goods1/{id}")
    public Goods findGoodsById1(@PathVariable("id") int id) {
        System.out.println("findGoodsById..." + id);
        
        //
        List<ServiceInstance> list = discoveryClient.getInstances("eureka-provider");
        if (list == null || list.isEmpty()) {
            return null;
        }
        ServiceInstance instance = list.get(0);
        String host = instance.getHost();
        System.out.println(host);
        int port = instance.getPort();
        System.out.println(port);
        //远程调用Goods服务中的findOne接口,方法1,通过
        String uri = "http://" + host + ":" + port + "/goods/findOne/1";
        Goods goods = restTemplate.getForObject(uri, Goods.class);
        return goods;
    }
    
    /**
     * 使用 Ribbon 简化restTemplate调用
     * 1. 在声明restTemplate的Bean时候，添加一个注解：@LoadBalanced
     * 2. 在使用restTemplate发起请求时，需要定义url时， host:port可以替换为 服务提供方的 应用名称
     *
     * @param id
     * @return
     */
    @GetMapping("/goods/{id}")
    public Goods findGoodsById(@PathVariable("id") int id) {
        System.out.println("findGoodsById..." + id);
        
        //• 随机 ： RandomRule
        // • 轮询 ： RoundRobinRule
        // • 最小并发： BestAvailableRule
        // • 过滤： AvailabilityFilteringRule
        // • 响应时间： WeightedResponseTimeRule
        // • 轮询重试： RetryRule
        // • 性能可用性： ZoneAvoidanceRule
        
        //远程调用Goods服务中的findOne接口,方法1,通过
        String uri = "http://eureka-provider/goods/findOne/1";
        Goods goods = restTemplate.getForObject(uri, Goods.class);
        return goods;
    }
}
